<?php

namespace App\Http\Requests;

use App\Entities\ConvertibleCardEntity;
use Illuminate\Foundation\Http\FormRequest;

class PurchaseConvertibleCardRequest extends FormRequest
{
    use BaseRequestTrait;

    /**
     * @var ConvertibleCardEntity
     */
    private $convertibleCardEntity;

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'convertibleCardId' => [
                'bail',
                'required',
                function ($attribute, $value, $fail) {
                    $convertibleCardEntity = ConvertibleCardEntity::find($value);
                    if (is_null($convertibleCardEntity)) {
                        $fail('权益卡不存在');
                    }
                    $this->convertibleCardEntity = $convertibleCardEntity;
                },
            ],
        ];
    }

    public function messages()
    {
        return [
        ];
    }

    /**
     * @return ConvertibleCardEntity
     */
    public function getConvertibleCardEntity(): ConvertibleCardEntity
    {
        return $this->convertibleCardEntity;
    }
}
